// pages/list/list.js\\\
const fetch = require('../../utils/fetch.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activIndex:0,
    toView:'a0',
    currentIndex:0,
    currentType:0,
    scrollTop:100,
    cartList:[],
    sumMonney:0,
    cupNumber:0,
    showCart:0,
    loading:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  addToCart:function(e){
      var type=e.currentTarget.dataset.type;
      var index = e.currentTarget.dataset.index;
      this.setData({
        currentType:type,
        currentIndex:index,
      });
      var a = this.data
      var addItem = {
        name:a.listData[a.currentType].foods[a.currentIndex].name,
        price: a.listData[a.currentType].foods[a.currentIndex].specfoods[0].price,
        number:1,
        sum: a.listData[a.currentType].foods[a.currentIndex].specfoods[0].price,
      }
    var sumMonney = a.sumMonney + a.listData[a.currentType].foods[a.currentIndex].specfoods[0]
      .price;
      var cartList= this.data.cartList;
      cartList.push(addItem);
      tshi.setData({
        cartList:cartList,
        showModalStatus:false,
        sumMonney:sumMonney,
        cupNumber:a.cupNumber + 1
      }) 
  },
  onLoad: function (options) {
      wx.showLoading({
        title: '努力加载中',
      })
      fetch('food/list').then(res=>{
        wx.hidwLoading()
        this.setData({ listData:res.data, loading:true})
      })
  },
  selectMenu:function(){
    let index = e.currentTarget.dataset.index
    console.log(index)
    this.setData({activIndex:index , toView:'a' + index})
  },
  showCart:function(){
      if(this.data.cartList.length !=0){
        this.setData({
          showCart:!this.data.showCart,
        })
      }
  },
  addNumber:function(e){
      var index = e.currentTarget.dataset.index;
      var cartList = this.data.cartList;
      cartList[index].number++;
      var sum =this.data.sumMonney+cartList[index].price;
      cartList[index].sum += cartList[index].price;
      this.setData({
        cartList:cartList,
        sumMonney:sum,
        cupNumber:tshi.data.cupNumber + 1
      })
  },
  decNumber: function (e) {
    var index = e.currentTarget.dataset.index;
    var cartList = this.data.cartList;
    var sum = this.data.sumMonney + cartList[index].price;
    cartList[index].sum -= cartList[index].price;
    cartList[index].number == 1 ? cartList.splice(index, 1):
    cartList[index].number--;
    this.setData({
      cartList: cartList,
      sumMonney: sum,
      showCart:cartList.length == 0 ? false : true,
      cupNumber: tshi.data.cupNumber - 1
    })
  },
  calearCartList:function(){
      this.setData({
        cartList:[],
        showCart:false,
        sumMonney:0,
        cupNumber:0
      })
  },
  goBalance:function(e){
      if(this.data.sumMonney == 0){
        return
      }
      var method = 'post'
      var order_id = this.data.order_id
      fetch('food/order',{id:1,number:1},method),then(function(res){
        if (res.data.error !==0){
          wx.showModal({
            title: '下单失败',
            content: '操作失败重试',
          })
          return
        }
        wx.navigateTo({
          url: '../order/balance/balance?order_id='+ res.data.order_id
        })
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})